﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.Plc;
using AutoFlatnessTest.ViewModel;
using Mapster;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// UcProject.xaml 的交互逻辑
    /// </summary>
    public partial class UcProject : UserControl
    {
        UcProjectVModel VModel = new UcProjectVModel();

        public UcProject()
        {
            InitializeComponent();
            this.DataContext = VModel;
        }

        private void load(object sender, RoutedEventArgs e)
        {
            //下拉框
            if (!WpfHelp.IsInDesignMode(this))
            {
                VModel.Proj = ProjectDb.GetNamsAll().ToDictionary(k => k.Id, v => v.Name);

                RunPlc();
            }

        }

        private void shuxin(object sender, RoutedEventArgs e)
        {
            RunPlc();
        }

        #region 下拉框 改变 增删改查
        private void selectChang(object sender, SelectionChangedEventArgs e)
        {
            var che = ((ComboBox)sender).SelectedValue?.ToString();
            VModel.SelectNoNull = che != null;

            if (che == null)
            {
                VModel.RowNum = null;
                VModel.ColumnNum = null;
                VModel.HomeVis = false;
                VModel.OkVal = null;
                VModel.OkValWarn = null;
                VModel.OneWidth = null;
                VModel.OneYz1 = null;
                VModel.OneYz2 = null;
                VModel.OneSmi1 = null;
                VModel.OneSmi2 = null;
                VModel.OneSma1 = null;
                VModel.OneSma2 = null;
                VModel.TwoYz1 = null;
                VModel.TwoYz2 = null;
                VModel.TwoSmi1 = null;
                VModel.TwoSmi2 = null;
                VModel.TwoSma1 = null;
                VModel.TwoSma2 = null;
                VModel.Sort = null;
            }
            else
            {
                var proj = ProjectDb.Get(che);

                VModel.RowNum = proj.RowNum;
                VModel.ColumnNum = proj.ColumnNum;
                VModel.HomeVis = proj.HomeShow == 1;
                VModel.IsSma = proj.IsSma == 1;
                VModel.IsCheck = proj.IsCheck == 1;
                VModel.OkVal = proj.OkVal;
                VModel.OkValWarn = proj.OkValWarn;
                VModel.OneWidth = proj.OneWidth;
                VModel.OneYz1 = proj.OneYz1;
                VModel.OneYz2 = proj.OneYz2;
                VModel.OneSmi1 = proj.OneSmi1;
                VModel.OneSmi2 = proj.OneSmi2;
                VModel.OneSma1 = proj.OneSma1;
                VModel.OneSma2 = proj.OneSma2;
                VModel.TwoYz1 = proj.TwoYz1;
                VModel.TwoYz2 = proj.TwoYz2;
                VModel.TwoSmi1 = proj.TwoSmi1;
                VModel.TwoSmi2 = proj.TwoSmi2;
                VModel.TwoSma1 = proj.TwoSma1;
                VModel.TwoSma2 = proj.TwoSma2;
                VModel.Sort = proj.Sort;
            }
        }

        private void po_xz(object sender, RoutedEventArgs e)
        {
            WinInput winInput = new WinInput();
            winInput.VModel.TxtTitle = "新增项目";
            winInput.VModel.TxtTextBox1 = "名称";
            winInput.VModel.TxtTextBox2 = "请输入新增项目名称";
            winInput.ClickOk += (object sender1, ClickOkEventInfo okInfo) =>
            {
                if (string.IsNullOrWhiteSpace(okInfo.InputTxt))
                {
                    okInfo.IsOk = false;
                    okInfo.ErrInfo = "输入信息不能为空";
                }
                else
                {
                    var ex = ProjectDb.GetNamsAll().Select(o => o.Name).Contains(okInfo.InputTxt);
                    if (ex)
                    {
                        okInfo.IsOk = false;
                        okInfo.ErrInfo = "此名称已经存在";
                    }
                    else
                    {
                        ProjectDb.Add(okInfo.InputTxt);
                    }
                }
            };

            if (winInput.ShowDialog() == true)
            {
                VModel.Proj = ProjectDb.GetNamsAll().ToDictionary(k => k.Id, v => v.Name);
                combox.SelectedItem = VModel.Proj.First(o => o.Value == winInput.VModel.InputTxt);
            }

        }

        private void po_sc(object sender, RoutedEventArgs e)
        {
            var che = combox.SelectedValue?.ToString();
            if (string.IsNullOrWhiteSpace(che))
            {
                MessageBox.Show("没有选择项");
            }
            else
            {
                var issc = MessageBox.Show("确定删除吗？", "删除提示！！", MessageBoxButton.OKCancel);
                if (issc == MessageBoxResult.OK)
                {
                    ProjectDb.Delect(che);

                    VModel.Proj = ProjectDb.GetNamsAll().ToDictionary(k => k.Id, v => v.Name);
                    combox.SelectedItem = null;
                }

            }
        }

        private void po_gm(object sender, RoutedEventArgs e)
        {
            var che = combox.SelectedValue?.ToString();
            if (string.IsNullOrWhiteSpace(che))
            {
                MessageBox.Show("没有选择项");
                return;
            }

            try
            {
                WinInput winInput = new WinInput();
                winInput.VModel.TxtTitle = "项目更名";
                winInput.VModel.TxtTextBox1 = "新名称";
                winInput.VModel.TxtTextBox2 = "请输入新名称";
                winInput.ClickOk += (object sender1, ClickOkEventInfo okInfo) =>
                {
                    if (string.IsNullOrWhiteSpace(okInfo.InputTxt))
                    {
                        okInfo.IsOk = false;
                        okInfo.ErrInfo = "输入信息不能为空";
                    }
                    else
                    {
                        var ex = ProjectDb.GetNamsAll().Select(o => o.Name).Contains(okInfo.InputTxt);
                        if (ex)
                        {
                            okInfo.IsOk = false;
                            okInfo.ErrInfo = "此名称已经存在";
                        }
                        else
                        {
                            ProjectUpdataNameDto projectUpdataNameDto = new ProjectUpdataNameDto();
                            projectUpdataNameDto.Id = che;
                            projectUpdataNameDto.Name = okInfo.InputTxt;
                            ProjectDb.UpdataName(projectUpdataNameDto);
                        }
                    }
                };

                if (winInput.ShowDialog() == true)
                {
                    VModel.Proj = ProjectDb.GetNamsAll().ToDictionary(k => k.Id, v => v.Name);
                    combox.SelectedItem = VModel.Proj.First(o => o.Value == winInput.VModel.InputTxt);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("更名失败" + ex.Message);
            }
        }

        private void po_bc(object sender, RoutedEventArgs e)
        {
            var che = combox.SelectedValue?.ToString();
            if (string.IsNullOrWhiteSpace(che))
            {
                MessageBox.Show("没有选择项");
            }
            else
            {
                var destObject = VModel.Adapt<ProjectUpdataDto>();

                destObject.HomeShow = VModel.HomeVis ? 1 : 2;
                destObject.IsSma = VModel.IsSma ? 1 : 2;
                destObject.IsCheck = VModel.IsCheck ? 1 : 2;
                destObject.Id = che;
                ProjectDb.Updata(destObject);

                MessageBox.Show("已保存");
            }

        }
        #endregion


        #region Plc 信息

        private void RunPlc()
        {
            if (MainWindow.SelectItemNum != 52)
                return;

            InovanceRun.Plc?.SetData<Int16>(InovanceComm.模式, 1);

            Task.Run(() =>
            {
                VModel.Vis = Visibility.Visible;

                while (true)
                {
                    if (MainWindow.SelectItemNum != 52)
                        break;

                    VModel.VisLjck = Visibility.Hidden;
                    Thread.Sleep(300);

                    try
                    {
                        VModel.YzLoca = (int)InovanceRun.Plc.GetData<Int32>(InovanceComm.运载当前位置);
                        VModel.SMiLoca = (int)InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪当前位置);
                        VModel.SMaLoca = (int)InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪当前位置);

                        Thread.Sleep(800);
                    }
                    catch (Exception ex)
                    {
                        VModel.VisLjck = Visibility.Visible;
                        VModel.YzLoca = null;
                        VModel.SMiLoca = null;
                        VModel.SMaLoca = null;
                    }
                    finally
                    {
                        VModel.Vis = Visibility.Collapsed;
                    }

                    if (VModel.VisLjck == Visibility.Visible)
                        break;

                }

            });
        }

        #endregion

        #region 定位
        private void yzzd(object sender, RoutedEventArgs e)
        {
            try
            {
                if (VModel.YzLocaZd == "0")
                {
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.运载回原, 2);
                }
                else
                {
                    DataTable dataTable = new DataTable();
                    var ysjg = Convert.ToInt32(dataTable.Compute(VModel.YzLocaZd, string.Empty));
                    VModel.YzLocaZd = ysjg.ToString();

                    InovanceRun.Plc.SetData<Int32>(InovanceComm.运载定位位置, ysjg);
                    Thread.Sleep(100);
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.运载定位位置执行, 2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"执行到指定位置失败：{ex.Message}");
            }
        }

        private void smizd(object sender, RoutedEventArgs e)
        {
            try
            {
                if (VModel.SMiLocaZd == "0")
                {
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.扫描仪回原, 2);
                }
                else
                {
                    DataTable dataTable = new DataTable();
                    var ysjg = Convert.ToInt32(dataTable.Compute(VModel.SMiLocaZd, string.Empty));
                    VModel.SMiLocaZd = ysjg.ToString();

                    InovanceRun.Plc.SetData<Int32>(InovanceComm.扫描仪定位位置, ysjg);
                    Thread.Sleep(100);
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.扫描仪定位位置执行, 2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"执行到指定位置失败：{ex.Message}");
            }
        }

        private void smazd(object sender, RoutedEventArgs e)
        {
            try
            {
                if (VModel.SMaLocaZd == "0")
                {
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.扫码枪回原, 2);
                }
                else
                {
                    DataTable dataTable = new DataTable();
                    var ysjg = Convert.ToInt32(dataTable.Compute(VModel.SMaLocaZd, string.Empty));
                    VModel.SMaLocaZd = ysjg.ToString();

                    InovanceRun.Plc.SetData<Int32>(InovanceComm.扫码枪定位位置, ysjg);
                    Thread.Sleep(100);
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.扫码枪定位位置执行, 2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"执行到指定位置失败：{ex.Message}");
            }
        }
        #endregion

        #region 扫描

        private void smidu(object sender, RoutedEventArgs e)
        {
            try
            {
                VModel.Smy = JiEnShiRun.DeviceSmi.SendWait(JiEnShiRun.SmiGo);
            }
            catch (Exception ex)
            {
                VModel.Smy = "失败";
            }
        }

        private void smitin(object sender, RoutedEventArgs e)
        {

        }

        private void smadu(object sender, RoutedEventArgs e)
        {
            try
            {
                VModel.Smq = JiEnShiRun.DeviceSma.SendWait(JiEnShiRun.SmaGo);
            }
            catch (Exception ex)
            {
                VModel.Smq = "失败";
            }
        }

        private void smatin(object sender, RoutedEventArgs e)
        {
            try
            {
                JiEnShiRun.DeviceSmi.SendWait(JiEnShiRun.SmaEnd);
            }
            catch (Exception ex)
            {
                MessageBox.Show("失败");
            }

        }
        #endregion


        #region 点击动
        private void yzdax1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.运载手动正向Jog, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void yzdsk1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.运载手动正向Jog, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void yzdax2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.运载手动反向Jog, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void yzdsk2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.运载手动反向Jog, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMidax1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫描仪手动正向Jog, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMidsk1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫描仪手动正向Jog, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMidax2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫描仪手动反向Jog, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMidsk2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫描仪手动反向Jog, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMadax1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫码枪手动正向Jog, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMadsk1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫码枪手动正向Jog, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMadax2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫码枪手动反向Jog, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }

        private void sMadsk2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                InovanceRun.Plc.SetData<Int16>(InovanceComm.扫码枪手动反向Jog, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误：{ex.Message}");
            }
        }
        #endregion

    }
}
